<?php


namespace api\app\controller;


use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use cmf\controller\RestBaseController;
use Darabonba\OpenApi\Models\Config;

/**
 * Class Sms
 * 短信功能示例
 */
class Sms extends RestBaseController
{
    // 阿里云短信发送示例
    // 文档地址：https://api.aliyun.com/api-tools/sdk/Dysmsapi?version=2017-05-25&language=php-tea&tab=primer-doc
    // 新用户优惠购：https://t.aliyun.com/U/3Fupv7
    public function sendAliSms()
    {
        $mobile=input('mobile');
        $config = new Config([
            // 从阿里云获取
            'accessKeyId' => config('app.aliSms.accessKeyId'),
            'accessKeySecret' => config('app.aliSms.accessKeySecret'),
        ]);

        $config->endpoint = "dysmsapi.aliyuncs.com";
        $client = new Dysmsapi($config);

        // 运行时参数超时设置，仅对使用了该运行时参数实例的请求有效
        $runtimeOptions = new RuntimeOptions();
        // true 忽略证书校验
        $runtimeOptions->ignoreSSL = true;

        $sendSmsRequest = new SendSmsRequest();
        // 手机号
        $sendSmsRequest->phoneNumbers = $mobile;
        // 签名名称
        $sendSmsRequest->signName = '阿里云短信测试';
        // 模板CODE
        $sendSmsRequest->templateCode = 'SMS_154950909';
        // 生成随机数作为验证码
        $sendSmsRequest->templateParam = json_encode(['code' => mt_rand(1111, 9999)]);
        try {
            $response = $client->sendSmsWithOptions($sendSmsRequest, $runtimeOptions);
        } catch (\Exception $exception) {
            halt($exception->getMessage());
        }
        if ($response->body->code == 'OK') {
            echo '短信发送成功！';

        }
    }
}